//////////////////////////////////////////////////////////////////////
//
//	Copyright 2010-2011 Rogo's Team. All Rights Reserved
//
//////////////////////////////////////////////////////////////////////
package com.rogo.weibo.platform.netease.api.status
{
	import com.rogo.weibo.core.DoAPI;
	import com.rogo.weibo.platform.netease.api.oauth.Oauth;
	import com.rogo.weibo.utils.HttpParameter;
	import com.rogo.weibo.utils.OauthUrlUtil;
	
	import flash.net.URLRequestMethod;
	
	import json.JSON;
	
	/**
	 * 网易微博API 
	 * 1.1.2 微博
	 * 
	 * @see http://rogo.sinaapp.com
	 * @see http://open.t.163.com
	 * @author rogo
	 */	
	public class DoStatus extends DoAPI
	{
		public function DoStatus(dataHandler:Function, errorHandler:Function)
		{
			super(dataHandler, errorHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		private static const URL_STATUS_UPDATE:String = 'http://api.t.163.com/statuses/update.json';
		public static const CMD_STATUS_UPDATE:String = 'cmdStatusUpdate';
		
		private static const URL_STATUS_REPLY:String = 'http://api.t.163.com/statuses/reply.json';
		public static const CMD_STATUS_REPLY:String = 'cmdStatusReply';
		
		private static const URL_STATUS_RETWEET:String = 'http://api.t.163.com/statuses/retweet/id.json';
		public static const CMD_STATUS_RETWEET:String = 'cmdStatusRetweet';
		
		private static const URL_STATUS_SHOW:String = 'http://api.t.163.com/statuses/show/id.json';
		public static const CMD_STATUS_SHOW:String = 'cmdStatusShow';
		
		private static const URL_STATUS_DESTROY:String = 'http://api.t.163.com/statuses/destroy/id.json';
		public static const CMD_STATUS_DESTROY:String = 'cmdStatusDestroy';
		
		private static const URL_STATUS_COMMENTS:String = 'http://api.t.163.com/statuses/comments/id.json';
		public static const CMD_STATUS_COMMENTS:String = 'cmdStatusComments';
		
		private static const URL_GET_STATUS_RETWEETS:String = 'http://api.t.163.com/statuses/retweets/id.json';
		public static const CMD_GET_STATUS_RETWEETS:String = 'cmdGetStatusRetweets';
		
		private static const URL_GET_RETWEETS_USER:String = 'http://api.t.163.com/statuses/id/retweeted_by.json';
		public static const CMD_GET_RETWEETS_USER:String = 'cmdGetRetweetsUser';
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 发布一条新微博
		 * 
		 * @param status 必选参数，微博内容，不得超过163个字符
		 * @param lat 可选参数，即纬度。范围在+90.0 到 -90.0，如果超出这个范围则忽略该值，必须与long参数一起使用，否则忽略该值。只有当用户的geo_enable为true时才有效
		 * @param long 可选参数,即经度。范围在+180.0到-180.0，如果超出这个范围则忽略该值，必须与lat参数一起使用，否则忽略该值。只有当用户的geo_enable为true时才有效
		 * @param vid 可选参数,即 POI 的 ID，某一个具体的地点，比如天安门
		 */		
		public function postStatus(status:String, lat:String, long:String, vid:String):void
		{
			var paras:Array = [];
			if (status != null)
			{
				status = encodeURI(status);
				paras.push(new HttpParameter('status', status));
			}
			if (lat != null)
				paras.push(new HttpParameter('lat', lat));
			if (long != null)
				paras.push(new HttpParameter('long', long));
			if (vid != null)
				paras.push(new HttpParameter('vid', vid));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			
			paras = OauthUrlUtil.getOauthPostParas(URL_STATUS_UPDATE, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParam:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_STATUS_UPDATE, 
				resultHandler, 
				errorHandler, 
			    URLRequestMethod.POST,
				postParam);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_STATUS_UPDATE, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_STATUS_UPDATE, params);
			}
		}
		
		/**
		 * 评论一条微博
		 * 
		 * @param id 必选参数，值为被评论微博的ID。如果回复某条评论，则此值为该评论的id
		 * @param status 必选参数，评论内容
		 * @param is_retweet 可选参数，是否转发 默认不转发 1为转发
		 * @param is_comment_to_root 是否评论给原微博 默认不评论 1为评论
		 */		
		public function replyStatus(id:String, status:String, is_retweet:String, is_comment_to_root:String):void
		{
			var paras:Array = [];
			if (id != null)
				paras.push(new HttpParameter('id', id));
			if (status != null)
				paras.push(new HttpParameter('status', status));
			if (is_retweet != null)
				paras.push(new HttpParameter('is_retweet', is_retweet));
			if (is_comment_to_root != null)
				paras.push(new HttpParameter('is_comment_to_root', is_comment_to_root));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			
			paras = OauthUrlUtil.getOauthPostParas(URL_STATUS_REPLY, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParam:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_STATUS_REPLY, 
				resultHandler, 
				errorHandler, 
				URLRequestMethod.POST,
				postParam);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_STATUS_REPLY, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_STATUS_REPLY, params);
			}
		}
		
		/**
		 * 转发一条微博
		 * 
		 * @param id 必选参数，值为被转发微博的ID
		 * @param status 可选参数，评论内容，默认为“转发微博
		 * @param is_retweet 可选参数，是否评论 默认不评论 1为评论
		 * @param is_comment_to_root 可选参数，是否评论给原微博 默认不评论 1为评论
		 */		
		public function retweetStatus(id:String, status:String, is_retweet:String, is_comment_to_root:String):void
		{
			var paras:Array = [];
			if (id != null)
				paras.push(new HttpParameter('id', id));
			if (status != null)
				paras.push(new HttpParameter('status', status));
			if (is_retweet != null)
				paras.push(new HttpParameter('is_retweet', is_retweet));
			if (is_comment_to_root != null)
				paras.push(new HttpParameter('is_comment_to_root', is_comment_to_root));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			
			paras = OauthUrlUtil.getOauthPostParas(URL_STATUS_RETWEET, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParam:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_STATUS_RETWEET, 
				resultHandler, 
				errorHandler, 
				URLRequestMethod.POST,
				postParam);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_STATUS_RETWEET, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_STATUS_RETWEET, params);
			}
		}
		
		/**
		 * 获取指定微博信息
		 * 
		 * @param id 必选参数，值为被转发微博的ID
		 */		
		public function showStatus(id:String):void
		{
			var paras:Array = [];
			if (id != null)
				paras.push(new HttpParameter('id', id));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_STATUS_SHOW, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_STATUS_SHOW, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_STATUS_SHOW, params);
			}
		}
		
		/**
		 * 删除指定的微博
		 * 
		 * @param id 必选参数，值为删除或撤销转发微博的ID
		 */		
		public function destroyStatus(id:String):void
		{
			var paras:Array = [];
			if (id != null)
				paras.push(new HttpParameter('id', id));
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			paras = OauthUrlUtil.getOauthPostParas(URL_STATUS_DESTROY, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParam:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_STATUS_DESTROY, resultHandler, errorHandler, URLRequestMethod.POST, postParam);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_STATUS_DESTROY, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_STATUS_DESTROY, params);
			}
		}
		
		/**
		 * 获取指定微博的评论列表
		 * 
		 * @param id 必选参数，指定的微博id
		 * @param count 可选参数，数量，默认为30条，最大为200条
		 * @param since_id 可选参数，该参数需传微博id，返回此条索引之前发的微博列表，不包含此条
		 * @param max_id 可选参数，该参数需传微博id，返回此条微博之后发的微博列表，包含此条
		 * @param trim_user 可选参数，值为true时返回的user对象只包含id属性，该参数能在一定程度上减少返回的数据量
		 */				
		public function getStatusComments(id:String, count:String, since_id:String, max_id:String, trim_user:Boolean = false):void
		{
			var paras:Array = [];
			if (id != null)
				paras.push(new HttpParameter('id', id));
			if (count != null)
				paras.push(new HttpParameter('count', count));
			if (since_id != null)
				paras.push(new HttpParameter('since_id', since_id));
			if (max_id != null)
				paras.push(new HttpParameter('max_id', max_id));
			paras.push(new HttpParameter('trim_user', trim_user.toString()));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_STATUS_COMMENTS, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_STATUS_COMMENTS, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_STATUS_COMMENTS, params);
			}
		}
		
		/**
		 * 获取指定微博的转发列表
		 * 
		 * @param id 必选参数，指定的微博id
		 * @param count 可选参数，数量，默认为30条，最大为200条
		 * @param since_id 可选参数，该参数需传微博id，返回此条索引之前发的微博列表，不包含此条
		 * @param max_id 可选参数，该参数需传微博id，返回此条微博之后发的微博列表，包含此条
		 * @param trim_user 可选参数，值为true时返回的user对象只包含id属性，该参数能在一定程度上减少返回的数据量
		 */	
		public function getStatusRetweets(id:String, count:String, since_id:String, max_id:String, trim_user:Boolean = false):void
		{
			var paras:Array = [];
			if (id != null)
				paras.push(new HttpParameter('id', id));
			if (count != null)
				paras.push(new HttpParameter('count', count));
			if (since_id != null)
				paras.push(new HttpParameter('since_id', since_id));
			if (max_id != null)
				paras.push(new HttpParameter('max_id', max_id));
			paras.push(new HttpParameter('trim_user', trim_user.toString()));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_GET_STATUS_RETWEETS, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_GET_STATUS_RETWEETS, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_GET_STATUS_RETWEETS, params);
			}
		}
		
		/**
		 * 获取一条微博被转发的用户信息
		 * 
		 * @param id 必选参数，值为被转发微博的ID
		 * @param count 可选参数，返回的用户数量，默认为100条，最大100条
		 */		
		public function getRetweetsUser(id:String, count:String):void
		{
			var paras:Array = [];
			if (id != null)
				paras.push(new HttpParameter('id', id));
			if (count != null)
				paras.push(new HttpParameter('count', count));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_GET_RETWEETS_USER, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_GET_RETWEETS_USER, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_GET_RETWEETS_USER, params);
			}
		}
		
		private function resultResponse(cmd:String, params:String):void
		{
			// decode json data result
			var data:Object = JSON.decode(params);
			_dataHandler(cmd, data);
		}
		
		private function errorResponse(cmd:String, params:String):void
		{
			// decode json data error
			var data:Object = JSON.decode(params);
			_errorHandler(cmd, data);
		}
		
	}
}